<cfcomponent displayname="CrafterDocumentMeta" accessors="yes" output="no">

	
	<!--- Properties --->
	<cfproperty name="productCode" type="string" />
	<cfproperty name="mode" type="string" />
	<cfproperty name="title" type="string" />
	<cfproperty name="keywords" type="string" />
	<cfproperty name="description" type="string" />
	<cfproperty name="canonicalLink" type="string" />
	<cfproperty name="hasLevelData" type="boolean" />
	<cfproperty name="levelTitle" type="string" />
	<cfproperty name="levelKeywords" type="string" />
	<cfproperty name="levelDescription" type="string" />
	<cfproperty name="levelCanonicalLink" type="string" />
	<cfproperty name="documentTitle" type="string" />
	<cfproperty name="documentKeywords" type="string" />
	<cfproperty name="documentDescription" type="string" />
	<cfproperty name="documentCanonicalLink" type="string" />
	
	
	<!--- init() --->
	<cffunction name="init" access="public" returntype="any" output="no">
		<cfargument name="productCode" type="string" required="yes" />
		<cfargument name="mode" type="string" required="yes" />
		<cfscript>
			setProductCode(arguments.productCode);
			setMode(arguments.mode);
			return this;
		</cfscript>
	</cffunction>
	
	
	<!--- load() --->
	<cffunction name="load" access="public" returntype="void" output="no">
		<cfargument name="xml" type="any" required="yes" />
		
		<cfscript>
		
			try {
				// Grab the level data.
				local.arLevelData = XmlSearch(arguments.xml, "//levelData/levelMeta");
				if (ArrayLen(local.arLevelData) GT 0) {
					setLevelTitle(local.arLevelData[1]["PageTitle"]["XmlText"]);
					setLevelKeywords(local.arLevelData[1]["PageKeywords"]["XmlText"]);
					setLevelDescription(local.arLevelData[1]["PageDescription"]["XmlText"]);
					setLevelCanonicalLink(local.arLevelData[1]["CanonicalLink"]["XmlText"]);
				}
				
				// Grab the document data.
				local.arDocumentData = XmlSearch(arguments.xml, "//metaGroup[productCode='#getProductCode()#']");
				if (ArrayLen(local.arDocumentData) GT 0) {
					setDocumentTitle(local.arDocumentData[1]["PageTitle"]["XmlText"]);
					setDocumentKeywords(local.arDocumentData[1]["PageKeywords"]["XmlText"]);
					setDocumentDescription(local.arDocumentData[1]["PageDescription"]["XmlText"]);
					setDocumentCanonicalLink(local.arDocumentData[1]["CanonicalLink"]["XmlText"]);
				}	
			}
			catch(Any e) {
				writedump(arguments.xml);
				writedump(e);
				abort;
			}

			
																 
	
			setFinalValues();
			
		</cfscript>
		
	</cffunction>
	
	
	<cffunction name="setFinalValues" access="private" returntype="void" output="no">

		
		<cfscript>
			switch (getMode()) {
				
				// Regardless of level values, use the values set
				// in the document itself.
				case "page-override":{
					setTitle(getDocumentTitle());
					setKeywords(getDocumentKeywords());
					setDescription(getDocumentDescription());
					setDescription(getDocumentCanonicalLink());
				}
				
				// Inherit from the level data when document data
				// is blank.  However, if the document data is not blank
				// than it is used.
				case "page-inherit":{
					
					if (getDocumentTitle() EQ "") {
						setTitle(getLevelTitle());	
					}
					else {
						setTitle(getDocumentTitle());	
					}
					
					if (getDocumentKeywords() EQ "") {
						setKeywords(getLevelKeywords());	
					}
					else {
						setKeywords(getDocumentKeywords());	
					}
					
					if (getDocumentDescription() EQ "") {
						setDescription(getLevelDescription());	
					}
					else {
						setDescription(getDocumentDescription());	
					}
					
					if (getDocumentCanonicalLink() EQ "") {
						setCanonicalLink(getLevelCanonicalLink());	
					}
					else {
						setCanonicalLink(getDocumentCanonicalLink());	
					}
				}
				
				
				// Append the page data to the level data
				case "page-append":{
					local.title = getLevelTitle() & " " & getDocumentTitle();
					local.keywords = ListPrepend(getDocumentKeywords(),getLevelKeywords());
					local.description = getLevelDescription() & " " & getDocumentDescription();
					local.canonicalLink = getLevelCanonicalLink() & " " & getDocumentCanonicalLink();
				}

			}
		</cfscript>
		
	</cffunction>

	
	<!--- overload() --->
	<cffunction name="overload" access="public" returntype="crafter.CrafterDocumentMeta" output="no">
		<cfargument name="oInMeta" type="crafter.CrafterDocumentMeta" required="yes" />

		<cfif (arguments.oInMeta.getTitle() NEQ "")>
			<cfset setTitle(arguments.oInMeta.getTitle()) />
		</cfif>
		
		<cfif (arguments.oInMeta.getKeywords() NEQ "")>
			<cfset setKeywords(arguments.oInMeta.getKeywords()) />
		</cfif>
		
		<cfif (arguments.oInMeta.getDescription() NEQ "")>
			<cfset setDescription(arguments.oInMeta.getDescription()) />
		</cfif>
		
		<cfif (arguments.oInMeta.getCanonicalLink() NEQ "")>
			<cfset setCanonicalLink(arguments.oInMeta.getCanonicalLink()) />
		</cfif>

		
		<!--- Returns for v1 compat --->
		<cfreturn this />
		
	</cffunction>
	
</cfcomponent>